package view;

import Dao.UserDao;
import Model.Obj.MemberCard;
import Model.OrcUtils.JDBCUtils;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SellGUI extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_5;
    private JTextField textField_6;
    private JRadioButton rdbtnNewRadioButton;
    private JRadioButton rdbtnNewRadioButton_1;
    private String cp = "";
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SellGUI frame = new SellGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SellGUI() {
        setTitle("\u552E\u5361\u7BA1\u7406");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 400, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("\u5361 \u53F7");

        textField = new JTextField();

        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("\u59D3 \u540D");

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        JButton btnNewButton = new JButton("注册会员");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButtonActionPerformed(e);
            }
        });

        JLabel lblNewLabel_2 = new JLabel("\u8BC1\u4EF6\u53F7");

        textField_2 = new JTextField();

        textField_2.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("\u6298 \u6263");

        JLabel lblNewLabel_4 = new JLabel("\u7C7B \u578B");

        textField_3 = new JTextField();

        textField_3.setColumns(10);

        JLabel lblNewLabel_5 = new JLabel("\u7535 \u8BDD");

        textField_5 = new JTextField();

        textField_5.setColumns(10);

        JLabel lblNewLabel_6 = new JLabel("\u91D1 \u989D");

        textField_6 = new JTextField();

        textField_6.setColumns(10);

        ButtonGroup group = new ButtonGroup();

        rdbtnNewRadioButton = new JRadioButton("\u50A8\u503C\u5361");
        rdbtnNewRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rdbtnNewRadioButtonActionPerformed(e);
            }
        });

        rdbtnNewRadioButton_1 = new JRadioButton("\u6298\u6263\u5361");
        rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rdbtnNewRadioButton_1ActionPerformed(e);
            }
        });

        group.add(rdbtnNewRadioButton);
        group.add(rdbtnNewRadioButton_1);

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(19)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(lblNewLabel_4, Alignment.TRAILING)
                                                .addComponent(lblNewLabel_3, Alignment.TRAILING)
                                                .addComponent(lblNewLabel_5, Alignment.TRAILING))
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(lblNewLabel))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(lblNewLabel_1)
                                                .addGap(18)
                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                                                        .addComponent(textField_5, Alignment.LEADING)
                                                        .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
                                                                .addComponent(rdbtnNewRadioButton)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(rdbtnNewRadioButton_1)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(lblNewLabel_6)))
                                                .addGap(18)
                                                .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(219, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(24)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(rdbtnNewRadioButton)
                                        .addComponent(rdbtnNewRadioButton_1)
                                        .addComponent(lblNewLabel_6)
                                        .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(40)
                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(124, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }

    private void rdbtnNewRadioButtonActionPerformed(ActionEvent e) {
        String temp = "储值卡";
        cp = temp;
        textField_6.setEnabled(true);
        textField_3.setEnabled(false);
    }

    private void rdbtnNewRadioButton_1ActionPerformed(ActionEvent e) {
        String temp = "折扣卡";
        cp = temp;
        textField_6.setEnabled(false);
        textField_3.setEnabled(true);
    }

    //注册用户
    private void btnNewButtonActionPerformed(ActionEvent e) {
        String CardNumber = textField.getText();
        String User_ID = textField_2.getText();
        String Name = textField_1.getText();
        String Phone = textField_5.getText();
        String Balance = textField_6.getText();
        String Discount = textField_3.getText();
        String CardStatus = "正常";

        if (CardNumber.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入卡号");
            return;
        }

        if (User_ID.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入证件号");
            return;
        }

        if (Name.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入姓名");
            return;
        }

        if (Phone.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入电话号码");
            return;
        }

        if (cp.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请选择卡类型");
            return;
        }
        if (cp.equals("储值卡")) {
            if (textField_6.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入存款金额");
                return;
            } else {
                Discount = "0";
            }
        }

        if (cp.equals("折扣卡")) {
            if (textField_6.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入折扣额度");
                return;
            } else {
                Balance = "0";
            }
        }
        Integer d = Integer.valueOf(Discount);
        Integer c = Integer.valueOf(Balance);
        int count = UserDao.ADDMEMBER(CardNumber, c, d, cp, Name, User_ID, Phone, CardStatus);

        if (count > 0) {
            JOptionPane.showMessageDialog(null, "会员卡注册完成");
        } else {
            JOptionPane.showMessageDialog(null, "会员卡注册失败");
        }
    }

}

